//
//  MemberOftheLibraryController.m
//  PASystem
//
//  Created by 刘沿 on 16/9/9.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "MemberOftheLibraryController.h"
#import "MemberModel.h"
#import "MemberTabCell.h"
#import "MemLibraryDetailController.h"

@interface MemberOftheLibraryController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation MemberOftheLibraryController

static NSString *const ID = @"MemberOftheLibraryCell";
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.title = @"委员库";
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];

}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': wykList,'service': LibrarieService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict1 = dict[@"attrs"];
            
            MemberModel *model = [MemberModel mj_objectWithKeyValues:dict1];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (void)MoreData{
    
    self.currentPage ++ ;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': wykList,'service': LibrarieService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict1 = dict[@"attrs"];
            
            MemberModel *model = [MemberModel mj_objectWithKeyValues:dict1];
            
            [self.dataArray addObject:model];
            
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MemberModel *model = self.dataArray[indexPath.row];
    
    MemberTabCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
   
    if (cell == nil) {
        
        cell = [MemberTabCell WithMemberTabCell];
    }
    
    cell.model = model;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MemberModel *model = self.dataArray[indexPath.row];
    
    MemLibraryDetailController *deVC = [[MemLibraryDetailController alloc] init];
    deVC.ID = model.ID;
    
    [self.navigationController pushViewController:deVC animated:YES];
}




@end

























